package com.neusoft.dibuy.myarray;

import java.util.Arrays;
import java.util.Map;

public class DemoApp01 {
    public static void main(String[] args) {

    }

// 假设队列中共有500人，每次从1开始数，数到3的人出队，下一个人接着从1开始数，到结尾后接着从头继续循环数，编写程序找到最后剩下的人是哪一个。
//
    public static void main7(String[] args) {
        int [] is = new int[500];
        for (int i = 0; i < is.length;  ) {
            is[i] = ++i;
        }
        int index = is.length-1;  //  精华
        int time = 0 ;
        while (  time < is.length ){
            for (int i = 0; i <  3; i++) {
                index = (index + 1) % is.length; //
                if ( is[index] == -1){  //
                    i--;
                }
            }
            System.out.println(is[index]);
            is[index] = -1;
            time++;
        }
    }

    public static void main6(String[] args) {
        int [] is1 = {1,3,5,7,9};
        int [] is2= {0,2,4,6,8,10,11,12,13};
        int [] ts = new int[ is1.length + is2.length  ];
        int index1 =0 ;
        int index2 = 0 ;
        int  index = 0 ;
        while (  index1 <  is1.length && index2 <  is2.length ){
            if ( is1[index1]  <  is2[index2] ){
                ts[index++] =  is1[index1++];
            }else {
                ts[index++] =  is2[index2++];
            }
        }
        if ( index1 ==  is1.length){
            System.arraycopy(   is2 , index2, ts ,index , is2.length - index2  );
        }else {
            System.arraycopy(   is1 , index1, ts ,index , is1.length - index1  );
        }

    }
    public static void main5(String[] args) {
        String s = "12321";
        String[] strings = s.split("");
//        strings.
        //  API
    }

    public static void main4(String[] args) {
        int[] is = {7,9,6,2,5,1,0,3,8,4};

        int [] ts1 = new int[3];  // 6,2,5
        System.arraycopy(is, 2, ts1, 0, 3);
        int [] ts2 = new int[30]; //   10  --- >  7,9,6,2,5,1,0,3,8,4
        System.arraycopy(is, 0, ts1, 10,  is.length );
        int [] ts3 = new int[30]; //   25   --- >  7,9,6,2,5,1,0,3,8,4
        System.arraycopy(is, 0, ts3, 25,    Math.min(ts3.length - 25,  is.length ));

    }

    public static void main3(String[] args) {
        int[]is ={0,1,2,3,4,5,6,7,8,9};
        int index = halfSearch(is, 5.5f);
        System.out.println(index);
    }

    private static int halfSearch(int[] is, float  i) {
        int  times = 0 ;
        int index =-1;
        int left =0 ;
        int right = is.length-1;
        int mid = 0;
        while (  right >=  left && index ==-1  ){
            mid = (right +  left) / 2;
            times++;
            if (  is[mid] ==i  ){
                index = mid;
                break;
            }else  {
                if( is[mid] > i   ){
                    right =  mid -1;
                }else{
                    left = mid +1;
                }
            }
        }
        System.out.println("查找次数："+times);
        return   index;

    }

    public static void main2(String[] args) {
        int[]is ={0,1,2,3,4,5,6,7,8,9};
//        L/2 ;  L/2

//
    }
    /**
     * TODO 无序查找
     * @param args
     */
    public static void main1(String[] args) {
        int[] is = {7,9,6,2,5,1,0,3,8,4};
//        期望： L/2;  L
        System.out.println(Arrays.toString(is));
//        高效
//        缺点：1、只读的; 2、没有下标
        for( int i : is   ){
            if( i == 5 ){
                System.out.println("找到了");
                break;
            }
        }
        int index =-1;
        for (int i = 0; i <is.length ; i++) {
            if (is[i]== 5){
                is[i] =999;
                index = i ;
                System.out.println("找到了");
                break;
            }
        }
    }
}
